<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2019/1/19
 * Time: 10:47
 */

namespace app\admin\controller;


use think\Db;

class Consumer extends Base
{
    /**
     * 用户首页展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $params = input('param.');
            $where = [];
            //认证状态
            if(isset($params['user_face']) && $params['user_face'] != ''){
                $where['user_face'] = $params['user_face'];
            }
            //手机号
            if(isset($params['key']) && $params['key'] != '')
            {
                $where['user_phone'] = ['like', "%" . $params['key'] . "%"];
            }
            $where['user_status'] = ['<>', 3];
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('u_user')->where($where)->count();// 获取总条数
            $lists = Db::name('u_user')
                ->join('up_area ua1', 'ua1.district_id = user_province', 'left')
                ->join('up_area ua2', 'ua2.district_id = user_city', 'left')
                ->join('up_area ua3', 'ua3.district_id = user_district', 'left')
                ->where($where)
                ->field('user_id, user_phone, user_username, user_face, last_login_time, user_status, 
                user_time, ua1.district as sheng, ua2.district as shi, ua3.district as qu')
                ->page($Nowpage,$limits)
                ->order('user_time desc')
                ->select();
            foreach($lists as $k => &$v){
                //做任务次数
                $num = Db::table('up_task_user')
                    ->where('tu_user_id', $v['user_id'])
                    ->where('tu_status', 2)
                    ->count();
                $user_id = $v['user_id'];
                $where1 = [
                    'mc_user_id' => $user_id,
                    'mc_send' => 1,
                ];
                $where2 = [
                    'mc_send_user_id' => $user_id,
                    'mc_send_status' => ['in','2,3']
                ];
                $couponnum = Db::table('up_my_shop_coupon')
                    ->where(function ($query) use($where1) {
                        $query->where($where1);
                    })
                    ->whereOr(function ($query) use($where2) {
                        $query->where($where2);
                    })
                    ->count();
                $v['num'] = $num;
                $v['couponnum'] = $couponnum;
                //省市区
                $v['address'] = $v['sheng'] . $v['shi'] . $v['qu'];
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        if(session('?admin_user_id')){
            session('admin_user_id',null);
        }
        return $this->fetch();
    }

    /*****
     * @return mixed
     * 查看详情
     */
    public function detail(){
        $id = input('param.id');
        $user_week_money = Db::table('up_u_user')->order('user_week_money desc')->column('user_id');
        $user_month_money = Db::table('up_u_user')->order('user_month_money desc')->column('user_id');
        $info =Db::name('u_user')->alias('a')
            ->join('up_area ua1', 'ua1.district_id = user_province', 'left')
            ->join('up_area ua2', 'ua2.district_id = user_city', 'left')
            ->join('up_area ua3', 'ua3.district_id = user_district', 'left')
            ->where('user_id',$id)
            ->field('a.*,ua1.district as sheng, ua2.district as shi, ua3.district as qu')
            ->find();
        $pm_week = array_search($info['user_id'],$user_week_money)+1;
        $pm_month = array_search($info['user_id'],$user_month_money)+1;
        if($info['user_wx_token']!=''){
            $info['wx_status'] = "已绑定";
        }else{
            $info['wx_status'] = "未绑定";
        }
        if($info['user_qq_token']!=''){
            $info['qq_status'] = "已绑定";
        }else{
            $info['qq_status'] = "未绑定";
        }
        if($info['user_wb_token']!=''){
            $info['wb_status'] = "已绑定";
        }else{
            $info['wb_status'] = "未绑定";
        }
        if($info['user_zfb_token']!=''){
            $info['zfb_status'] = "已绑定";
        }else{
            $info['zfb_status'] = "未绑定";
        }
//        if(!session('?admin_user_id')){
//            session('admin_user_id',input('id'));
//        }
        if($info['user_gender']==1){
            $info['sex']= '男';
        }else{
            $info['sex']= '女';
        }
        if($info['user_face']==1){
            $info['face']= '未认证';
        }else{
            $info['face']= '已认证';
        }

        $info['address']  = $info['sheng'].$info['shi'].$info['qu'];
        $info['pm_week']  = $pm_week;
        $info['pm_month']  = $pm_month;
        $this->assign('res',$info);
        return $this->fetch();
    }

    /****
     * @return mixed|\think\response\Json
     * 任务信息
     */
    public function task(){
        if(request()->isAjax ()){
            $id = input('param.id');
            $where['a.tu_user_id'] = $id;
            $where['a.tu_status'] = array('IN',array(1,2,4,5));
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('task_user')
                        ->alias('a')
                        ->join('up_task ut','ut.task_id = a.tu_task_id','left')
                        ->where($where)->count();// 获取总条数
            $lists = Db::name('task_user')->alias('a')
                ->join('up_task ut','ut.task_id = a.tu_task_id','left')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('a.tu_id desc')
                ->field('a.*,ut.task_name,ut.task_type,ut.task_money')
                ->select();
            foreach ($lists as $key=>&$vo){
                if($vo['task_type']==1){
                    $vo['task_type_name']='下载';
                }elseif ($vo['task_type']==2){
                    $vo['task_type_name']='求赞求粉';
                }elseif ($vo['task_type']==3){
                    $vo['task_type_name']='问卷';
                }elseif ($vo['task_type']==4){
                    $vo['task_type_name']='投票';
                }elseif ($vo['task_type']==5){
                    $vo['task_type_name']='视频';
                }elseif ($vo['task_type']=6){
                    $vo['task_type_name']='分享';
                }
                if($vo['tu_status']==1){
                    $vo['tu_status_name']='待审核';
                }elseif ($vo['tu_status']==2){
                    $vo['tu_status_name']='已完成';
                }elseif ($vo['tu_status']==4){
                    $vo['tu_status_name']='进行中';
                }elseif ($vo['tu_status']==5){
                    $vo['tu_status_name']='取消';
                }
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
         if(!session('?admin_user_id')){
             session('admin_user_id',input('id'));
          }
          return $this->fetch();
    }

    /****
     * @return mixed|\think\response\Json
     * 关注信息
     */
    public function follow(){
        if(request()->isAjax ()){
            $id = input('param.id');
            $where['a.follow_user_id'] = $id;
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('follow')
                ->alias('a')
                ->join('up_shops ut','ut.s_id = a.follow_s_id','left')
                ->where($where)->count();// 获取总条数
            $lists = Db::name('follow')
                ->alias('a')
                ->join('up_shops ut','ut.s_id = a.follow_s_id','left')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('a.follow_time desc')
                ->field('a.*,ut.s_name,ut.s_thumb,ut.s_task_num')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        if(!session('?admin_user_id')){
            session('admin_user_id',input('id'));
        }
        return $this->fetch();
    }

    /****
     * @return mixed|\think\response\Json
     * 投诉信息
     */
    public function complain(){
        if(request()->isAjax ()){
            $id = input('param.id');
            $where['a.complain_user_id'] = $id;
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('complain')
                ->alias('a')
                ->join('up_shops us','us.s_id = a.complain_shop_id','left')
                ->join('up_task ut','ut.task_id = a.complain_task_id','left')
                ->where($where)->count();// 获取总条数
            $lists = Db::name('complain')->alias('a')
                ->join('up_shops us','us.s_id = a.complain_shop_id','left')
                ->join('up_task ut','ut.task_id = a.complain_task_id','left')
                ->where($where)
                ->page($Nowpage,$limits)
                ->field('a.*,us.s_name,us.s_image,us.s_thumb,ut.task_number')
                ->select();
            foreach ($lists as $key=>&$vo){
                if($vo['complain_status']==1){
                    $vo['status'] = '待处理';
                }elseif ($vo['complain_status']==2){
                    $vo['status'] = '已处理';
                }elseif ($vo['complain_status']==3){
                    $vo['status'] = '忽略';
                }
                $vo['photo'] = explode(',',$vo['complain_img']);
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        if(!session('?admin_user_id')){
            session('admin_user_id',input('id'));
        }
        return $this->fetch();
    }

    /****
     * @return mixed|\think\response\Json
     * 发布广告信息
     */
    public function advert(){
        if(request()->isAjax ()){
            $id = input('param.id');
            $where['adv_user_id'] = $id;
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('advert')
                ->where($where)->count();// 获取总条数
            $lists = Db::name('advert')
                ->where($where)
                ->page($Nowpage,$limits)
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        if(!session('?admin_user_id')){
            session('admin_user_id',input('id'));
        }
        return $this->fetch();
    }

    /****
     * @return mixed|\think\response\Json
     * 申请达人信息
     */
    public function super(){
        if(request()->isAjax ()){
            $id = input('param.id');
            $where['sup_user_id'] = $id;
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('super')
                ->where($where)->count();// 获取总条数
            $lists = Db::name('super')
                ->where($where)
                ->page($Nowpage,$limits)
                ->select();
            foreach ($lists as $key=>&$vo){
                $up_phone = Db::table('up_phone')->value('up_phone');
                $vo['phone'] = $up_phone;
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        if(!session('?admin_user_id')){
            session('admin_user_id',input('id'));
        }
        return $this->fetch();
    }

    /****
     * @return mixed
     * 申请达人详情信息
     */
    public function det(){
        $id = input('param.id');
        $res =  Db::table('up_super')->where('sup_id',$id)->find();
        $up_phone = Db::table('up_phone')->value('up_phone');
        $res['phone'] = $up_phone;
        $this->assign('res',$res);
        return $this->fetch();
    }
    
    /**
     * 删除用户
     * @return \think\response\Json
     */
    public function del(){
        $id = input('id');
        $res = Db::table('up_u_user')->where('user_id', $id)->setField('user_status', 3);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    
    /**
     * 启用/冻结用户
     * @return \think\response\Json
     */
    public function memberState(){
        $id = input('id');
        $num = input('num');
        $res = Db::table('up_u_user')->where('user_id', $id)->setField('user_status', $num);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }

    /**
     * 用户卡券
     * @return mixed|\think\response\Json
     */
    public function usercoupon(){
        if(request()->isAjax()){
            $user_id = input('param.id');
            $where1 = [
                'mc_user_id' => $user_id,
                'mc_send' => 1,
            ];
            $where2 = [
                'mc_send_user_id' => $user_id,
                'mc_send_status' => ['in','2,3']
            ];
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_my_shop_coupon')->alias('c')
                ->join('up_shop_coupon sc','c.mc_coupon_id=sc.coupon_id','LEFT')
                ->join('up_shops s','c.mc_s_id=s.s_id','LEFT')
                ->join('up_u_user u','c.mc_user_id=u.user_id','LEFT')
                ->join('up_u_user u2','c.mc_send_user_id=u2.user_id','LEFT')
                ->where(function ($query) use($where1) {
                    $query->where($where1);
                })
                ->whereOr(function ($query) use($where2) {
                    $query->where($where2);
                })
                ->count();// 获取总条数
            $field = 'c.*';
            $field .= ',u.user_username,u.user_phone,u2.user_username user_username2,u2.user_phone user_phone2';
            $field .= ',coupon_name,coupon_image,coupon_price,coupon_money,s_name,s_phone';
            $lists = Db::table('up_my_shop_coupon')->alias('c')
                ->join('up_shop_coupon sc','c.mc_coupon_id=sc.coupon_id','LEFT')
                ->join('up_shops s','c.mc_s_id=s.s_id','LEFT')
                ->join('up_u_user u','c.mc_user_id=u.user_id','LEFT')
                ->join('up_u_user u2','c.mc_send_user_id=u2.user_id','LEFT')
                ->where(function ($query) use($where1) {
                    $query->where($where1);
                })
                ->whereOr(function ($query) use($where2) {
                    $query->where($where2);
                })
                ->order('mc_time DESC')
                ->page($Nowpage,$limits)
                ->field($field)
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $this->assign('id',input('id'));
        return $this->fetch();
    }
}